﻿using System;
using System.Globalization;
using System.Xml;
using System.Windows;
using System.Windows.Media;
using JiveMessenger.Utilities;

namespace JiveMessenger
{
    public class LogElement : IComparable<LogElement>
    {
        public int SessionID { get; set; }
        public DateTime TimeStamp { get; set; }
        public string From { get; set; }
        public string To { get; set; }
        public string Text { get; set; }
        private string Font { get; set; }
        private string FontStyle { get; set; }
        private string FontWeight { get; set; }
        private string ColorHex { get; set; }

        private string StyleString
        {
            get
            {
                string style = "font-family:" + this.Font + "; ";
                if (!String.IsNullOrEmpty(this.FontWeight))
                    style += "font-weight:" + this.FontWeight + "; ";
                if (!String.IsNullOrEmpty(this.FontStyle))
                    style += "font-style:" + this.FontStyle + "; ";

                style += "color:#" + this.ColorHex + "; ";

                return style;

            }
            set
            {
                string[] styles = value.Split(';');
                foreach (string str in styles)
                {
                    if (str.Contains("font-family"))
                        this.Font = str.Replace("font-family:", "").Replace(";", "").Trim();
                    else if (str.Contains("font-weight"))
                        this.FontWeight = str.Replace("font-weight:", "").Replace(";", "").Trim();
                    else if (str.Contains("font-style"))
                        this.FontStyle = str.Replace("font-style:", "").Replace(";", "").Trim();
                    else if (str.Contains("color"))
                    {
                        string hexvalue = str.Replace("color:", "").Replace(";", "").Replace("#", "").Trim();
                        this.ColorHex = hexvalue;
                    }
                }

            }
        }

        public int CompareTo(LogElement other)
        {
            return DateTime.Compare(this.TimeStamp, other.TimeStamp);
        }

        public LogElement(DateTime timeStamp, string from, string to, string text, FontFamily font, FontStyle fontStyle, FontWeight fontWeight, Color color)
        {
            this.TimeStamp = timeStamp;
            this.SessionID = 0;
            this.From = from;
            this.To = to;
            this.Text = text;
            this.Font = StaticMethods.ConvertToString(font);
            this.FontWeight = StaticMethods.ConvertToString(fontWeight);
            this.FontStyle = StaticMethods.ConvertToString(fontStyle);
            this.ColorHex = StaticMethods.ColorToHexString(color);
        }
        public LogElement()
        {

        }

        public bool IsOK()
        {
            return this.SessionID != -1 && !String.IsNullOrEmpty(this.Text) && !String.IsNullOrEmpty(this.To) && !String.IsNullOrEmpty(this.From) && this.TimeStamp != DateTime.MinValue;
        }

        public LogElement(XmlReader r)
        {
            this.SessionID = -1;
            r.Read();
            if (r.Name != "Message")
                return;
            DateTime timeStamp;

            if (DateTime.TryParse(r.GetAttribute("DateTime"), CultureInfo.InvariantCulture, DateTimeStyles.None, out timeStamp))
                this.TimeStamp = timeStamp;
            int id;
            if (!Int32.TryParse(r.GetAttribute("SessionID"), out id))
                return;
            this.SessionID = id;
            r.Read();
            while (!r.EOF)
            {
                if (r.Name.Equals("From"))
                {
                    XmlReader r2 = r.ReadSubtree();
                    r2.Read();
                    while (!r2.EOF)
                    {

                        if (r2.Name == "From" && !r2.IsStartElement())
                            break;
                        if (r2.Name == "User")
                            this.From = r2.GetAttribute("FriendlyName");
                        r2.Read();
                    }
                    r.Read();
                }
                else if (r.Name.Equals("To"))
                {
                    XmlReader r2 = r.ReadSubtree();
                    r2.Read();
                    while (!r2.EOF)
                    {

                        if (r2.Name == "To" && !r2.IsStartElement())
                            break;
                        if (r2.Name == "User")
                            this.To = r2.GetAttribute("FriendlyName");
                        r2.Read();
                    }
                    r.Read();
                }
                else if (r.Name == "Text")
                {
                    string txt = r.GetAttribute("Style");
                    this.Text = r.ReadElementContentAsString();

                    this.StyleString = txt;
                }
                else
                    if ((r.IsEmptyElement) || !r.IsStartElement())
                        r.ReadOuterXml();
                    else
                    {
                        r.Read();
                    }
            }
        }

        public void WriteXml(XmlWriter w)
        {


            w.WriteStartElement("Message");

            w.WriteAttributeString("Date", this.TimeStamp.ToShortDateString());
            w.WriteAttributeString("Time", this.TimeStamp.ToString("HH:mm:ss"));
            w.WriteAttributeString("DateTime", this.TimeStamp.ToString("MM/dd/yyyy HH:mm:ss.ffff", CultureInfo.InvariantCulture));
            w.WriteAttributeString("SessionID", this.SessionID.ToString(CultureInfo.InvariantCulture));

            w.WriteStartElement("From");
            w.WriteStartElement("User");
            w.WriteAttributeString("FriendlyName", this.From);
            w.WriteEndElement();//User
            w.WriteEndElement();//From

            w.WriteStartElement("To");
            w.WriteStartElement("User");
            w.WriteAttributeString("FriendlyName", this.To);
            w.WriteEndElement();//User
            w.WriteEndElement();//To


            w.WriteStartElement("Text");
            w.WriteAttributeString("Style", this.StyleString);
            w.WriteValue(this.Text);
            w.WriteEndElement();//Text
            w.WriteEndElement();//Message

        }


    }
}
